package com.jinsilu.freight.ui.fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.aop.SingleClick;
import com.jinsilu.freight.common.MyFragment;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.helper.ReplaceHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.UserInfoApi;
import com.jinsilu.freight.http.response.UserInfoBean;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.activity.BlankNoteActivity;
import com.jinsilu.freight.ui.activity.HelpCenterActivity;
import com.jinsilu.freight.ui.activity.HomeActivity;
import com.jinsilu.freight.ui.activity.ManageAccountActivity;
import com.jinsilu.freight.ui.activity.MyBankManageActivity;
import com.jinsilu.freight.ui.activity.MyVehicleManageActivity;
import com.jinsilu.freight.ui.activity.SuggestionsActivity;
import com.jinsilu.freight.ui.activity.UploadIdCardActivity;

public class MeFragment extends MyFragment<HomeActivity> {

    private LinearLayout mLayoutBlankNote;
    private ImageView meAvatar;
    private ImageView mIvCertificate;
    private TextView mName;
    private TextView mPhone;
    private TextView mCertificateStatus;

    private UserInfoBean userInfoBean;

    public static MeFragment newInstance() {
        return new MeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me_bak;
    }

    @Override
    protected void initView() {

        mLayoutBlankNote = findViewById(R.id.me_ll_my_protocol);
        meAvatar = findViewById(R.id.me_avatar);
        mName = findViewById(R.id.me_name);
        mPhone = findViewById(R.id.me_phone);
        mIvCertificate = findViewById(R.id.me_certificate);
        mCertificateStatus = findViewById(R.id.me_certificate_status);

//        if (CommonHelper.isShowBlankNote()) mLayoutBlankNote.setVisibility(View.VISIBLE);

        setOnClickListener(R.id.me_ll_my_certificate, R.id.me_ll_vehicle_manager, R.id.me_ll_bank_manage, R.id.me_ll_my_protocol,
                R.id.me_ll_help_center, R.id.me_ll_account_manage, R.id.me_ll_contact_customer, R.id.me_ll_complaint);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshUserData();
    }

    private void refreshUserData() {
        // 刷新用户信息
        EasyHttp.get(this)
                .api(new UserInfoApi())
                .request(new HttpCallback<HttpData<UserInfoBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<UserInfoBean> data) {
                        CommonHelper.saveObject("userInfo", data.getData());
                        initData();
                    }
                });
    }

    @Override
    protected void initData() {

        if (!CommonHelper.isLogin())
            return;

        userInfoBean = (UserInfoBean) CommonHelper.getSavedObject("userInfo", UserInfoBean.class);
        if (!ObjectUtils.isEmpty(userInfoBean)) {
            mPhone.setText(ReplaceHelper.phoneFormat(userInfoBean.getPhone()));
            mName.setText(TextUtils.isEmpty(userInfoBean.getName()) ? "未认证" : userInfoBean.getName());
            switch (userInfoBean.getCertificationStatus()) {
                case 0:
                    mCertificateStatus.setText("认证失败");
                    mIvCertificate.setImageResource(R.drawable.icon_failed_certification);
                    break;
                case 1:
                    mCertificateStatus.setText("认证成功");
                    mIvCertificate.setImageResource(R.drawable.me_certified_ic);
                    break;
                case -1:
                    mCertificateStatus.setText("认证中");
                    mIvCertificate.setImageResource(R.drawable.icon_certificating);
                    break;
                case -2:
                default:
                    mCertificateStatus.setText("未认证");
                    mIvCertificate.setImageResource(R.drawable.icon_no_certification);
                    break;
            }
        } else {
            mName.setText("");
            mPhone.setText("");
            mCertificateStatus.setText("未认证");
            mIvCertificate.setImageResource(R.drawable.icon_no_certification);
        }
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (!CommonHelper.isLogin()) {
            CommonHelper.goLoin();
            return;
        }
        int id = v.getId();
        if (id == R.id.me_ll_my_certificate) {
//            startActivity(MyCertificationActivity.class);
            startActivity(UploadIdCardActivity.class);
        } else if (id == R.id.me_ll_vehicle_manager) {
            startActivity(MyVehicleManageActivity.class);
        } else if (id == R.id.me_ll_bank_manage) {
            if (ObjectUtils.isEmpty(userInfoBean)) return;
            switch (userInfoBean.getCertificationStatus()) {
                case -2:
                    toast("请前往认证");
                    break;
                case 0:
                    toast("请重新认证");
                    break;
                case 1: // 审核通过
                    startActivity(MyBankManageActivity.class);
                    break;
                case -1:
                    toast("请等待审核通过");
                    break;
            }

        } else if (id == R.id.me_ll_my_protocol) {
            if (ObjectUtils.isEmpty(userInfoBean)) return;
            switch (userInfoBean.getCertificationStatus()) {
                case -2:
                    toast("请前往认证");
                    break;
                case 0:
                    toast("请重新认证");
                    break;
                case 1: // 审核通过
                    startActivity(BlankNoteActivity.class);
                    break;
                case -1:
                    toast("请等待审核通过");
                    break;
            }
        } else if (id == R.id.me_ll_help_center) {
            startActivity(HelpCenterActivity.class);
        } else if (id == R.id.me_ll_account_manage) {
            startActivity(ManageAccountActivity.class);
        } else if (id == R.id.me_ll_contact_customer) {
            try {
                PhoneUtils.dial(MTLConstants.CUSTOMER_PHONE_NUMBER);
            }catch (Exception e){
                e.printStackTrace();
            }

        } else if (id == R.id.me_ll_complaint) {
            startActivity(SuggestionsActivity.class);
        }
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }
}
